import 'package:hockey/widgets/common/jzx_toast.dart';
import 'package:url_launcher/url_launcher.dart';

class JzxLaunch {
  /// 打开网页
  static Future openUrl(String url,
      {LaunchMode model = LaunchMode.inAppWebView}) async {
    try {
      Uri launchUri;
      var path = url;
      if (url.endsWith('/')) {
        path = url.substring(0, url.length - 1);
      }
      final splitList = path.split('://');

      // if (isFile) {

      launchUri = Uri.parse(
          '${splitList.first == 'https' ? 'https' : 'http'}://${splitList.last}');
      // launchUri = Uri(
      //   scheme: splitList.first == 'https' ? 'https' : 'http',
      //   host: '',
      //   path: splitList.last,
      // );
      // } else {
      //   if (splitList.first == 'https') {
      //     launchUri = Uri.https(
      //       splitList.last,
      //     );
      //   } else {
      //     launchUri = Uri.http(
      //       splitList.last,
      //     );
      //   }
      // }

      if (await canLaunchUrl(launchUri)) {
        await launchUrl(launchUri, mode: model);
      }
    } catch (e) {
      // print(e);
      showMessage('$e');
    }
  }
}

String? encodeQueryParameters(Map<String, String> params) {
  return params.entries
      .map((MapEntry<String, String> e) =>
          '${Uri.encodeComponent(e.key)}=${Uri.encodeComponent(e.value)}')
      .join('&');
}
